clear all
set more off

set varabbrev off

******************************************************************************
* Specify working directory
******************************************************************************

* For example
* cd "/Users/.../XYZ/"

global rawdata "Data/Raw_Data"
global tempdata "Data/RevisionRFS/temp_data"
global masterdata "Data/Master_Data"
global revisiondata "Data/RevisionRFS"

clear all
set more off
set type double

cap log close

global figures = "Results/Figures"
global output = "Results/Output"
global revisionoutput = "Results/RevisionRFS/Output"

****************************************************************************************************
//Second Stage
****************************************************************************************************

/* Load data */
use "${revisiondata}/sample/Data1_us_europe_revision_rfs_v2_std.dta", clear

********************************************************************************
/* 1. Step: Pooled Regression */
********************************************************************************

/* Estimation */
forval i=1(1)7{
preserve
keep if type_new2==`i'

eststo: ivreghdfe LNrweight (yield = IVme_alt61_95) LNtimetomat spread LNbv rating_num coupon_dec if type_new2==`i' [aw=aum_tot], a(date#fundid) clus(fundid) nocollin 
 	estadd local fund_time_fe "Yes"
	
restore
}

/* Reporting */
esttab using "${revisionoutput}/pooled_results_alt6_robustness_ins", replace f booktabs ///
b(%9.3f) se(%9.3f) ar2(%9.3f) eqlabels(none) title(xx) ///
mgroups("Life" "P\&C" "Traditional" "Variable Annuity" "ETF" "Others \& Pension" "Foreign", pattern(1 1 1 1 1 1 1) prefix(\multicolumn{@span}{c}{) suffix(}) span erepeat(\cmidrule(lr){@span})) ///
stats(fund_time_fe N r2_a widstat, fmt(0 %15.0gc %9.2f %9.2f) ///
labels("Fund $\times$ Quarter Fixed Effects" "Observations" "Kleibergen-Paap F-statistic")) ///
star(* 0.10 ** 0.05 *** 0.01) ///
keep(yield LNtimetomat spread LNbv rating_num) ///
obslast label ///
addnotes(Standard errors are clustered at the bond level) ///
nonumbers mtitle("I" "II" "III" "IV" "V" "VI" "VII" "VIII" "IX" "X" "XI" "XII") ///
substitute(\_ _)
eststo clear


/* Load data */
use "${revisiondata}/sample/Data1_us_europe_revision_rfs_v2_std.dta", clear

drop yieldhat

gen yieldhat = .

label var yieldhat "$\overline{\textit{Yield}}_{n,t}$"  

********************************************************************************
/* 2. Step: Within Assetclass Regressions - Pooled First Stage */
********************************************************************************


forval i=1(1)7{
	
	reghdfe yield IVme_alt61_95 LNtimetomat spread LNbv rating_num coupon_dec if type_new2==`i' [aw=aum_tot], a(date#fundid) clus(fundid) resid
	
	predict yieldhat_type, xbd
	
	replace yieldhat = yieldhat_type if type_new2==`i'
	
	drop yieldhat_type

}


/* Estimation */
forval i=1(1)7{
preserve
	keep if type_new2==`i' 
	
	eststo: reghdfe LNrweight_conditional yieldhat LNtimetomat spread LNbv rating_num coupon_dec if type_new2==`i' & assetclass == 1 [aw=aum_assetclass_ac], a(date#fundid_ac) clus(fundid_ac) resid
	estadd local fund_time_fe "Yes"
	
	predict deltahat_1, xbd
	
	replace deltahat_1 =. if assetclass == 2
	
	eststo: reghdfe LNrweight_conditional yieldhat LNtimetomat spread LNbv rating_num coupon_dec if type_new2==`i' & assetclass == 2 [aw=aum_assetclass_ac] , a(date#fundid_ac) clus(fundid_ac) resid
 	estadd local fund_time_fe "Yes"
	
	predict deltahat_2, xbd
	
	replace deltahat_2 =. if assetclass == 1
	
	gen LNdeltahat = deltahat_1
	replace LNdeltahat = deltahat_2 if LNdeltahat ==. 
	
	keep date fundid bondid assetclass LNdeltahat type_new2
	
	save "${tempdata}/TMP_`i'_conditional_alt6_pooled_fs_robustness_ins", replace

restore
}

/* Reporting */
esttab using "${revisionoutput}/conditional_results_alt6_pooled_fs_robustness_ins", replace f booktabs ///
b(%9.3f) se(%9.3f) ar2(%9.3f) eqlabels(none) title(xx) ///
mgroups("Life" "P\&C" "Traditional" "Variable Annuity" "ETF" "Others \& Pension" "Foreign", pattern(1 1 1 1 1 1 1) prefix(\multicolumn{@span}{c}{) suffix(}) span erepeat(\cmidrule(lr){@span})) ///
stats(fund_time_fe N r2_a widstat, fmt(0 %15.0gc %9.2f %9.2f) ///
labels("Fund $\times$ Quarter Fixed Effects" "Observations" "Kleibergen-Paap F-statistic")) ///
star(* 0.10 ** 0.05 *** 0.01) ///
keep(yieldhat LNtimetomat spread LNbv rating_num) ///
obslast label ///
addnotes(Standard errors are clustered at the bond level) ///
nonumbers mtitle("I" "II" "III" "IV" "V" "VI" "VII" "VIII" "IX" "X" "XI" "XII") ///
substitute(\_ _)
eststo clear

preserve
********************************************************************************
/* 3. Step: Construct Instruments */
********************************************************************************

/* Load Data for Instrument */
use "${tempdata}/TMP_1_conditional_alt6_pooled_fs_robustness_ins", clear
append using "${tempdata}/TMP_2_conditional_alt6_pooled_fs_robustness_ins"
append using "${tempdata}/TMP_3_conditional_alt6_pooled_fs_robustness_ins"
append using "${tempdata}/TMP_4_conditional_alt6_pooled_fs_robustness_ins"
append using "${tempdata}/TMP_5_conditional_alt6_pooled_fs_robustness_ins"
append using "${tempdata}/TMP_6_conditional_alt6_pooled_fs_robustness_ins"
append using "${tempdata}/TMP_7_conditional_alt6_pooled_fs_robustness_ins"

gsort fundid date assetclass bondid

gen deltahat = exp(LNdeltahat)
gegen sum_deltahat = sum(deltahat), by(fundid date assetclass)

replace sum_deltahat = . if sum_deltahat ==0
gen LNoutweight_conditional_hat = ln(1+sum_deltahat)  

winsor2 LNoutweight_conditional_hat, replace cuts(1 99) by(date)

save "${tempdata}/TMP_instruments_alt6_pooled_fs_robustness_ins", replace

restore

********************************************************************************
/* 4. Step: Across Assetclass Regression */
********************************************************************************
 
merge 1:1 date fundid bondid assetclass using "${tempdata}/TMP_instruments_alt6_pooled_fs_robustness_ins"


duplicates drop date fundid assetclass, force

keep date fundid assetclass outweight_conditional LNoutweight_conditional_hat aum_assetclass aum_tot type_new2

gegen id = group(date fundid)
reshape wide outweight_conditional LNoutweight_conditional_hat aum_assetclass aum_tot, i(id) j(assetclass)
	
gen LNweight_assetclass = ln(aum_assetclass1/aum_assetclass2)
gen LNoutweight_conditional1 = -ln(outweight_conditional1)
gen LNoutweight_conditional2 = ln(outweight_conditional2)
replace LNoutweight_conditional_hat1 = -LNoutweight_conditional_hat1
replace LNoutweight_conditional_hat2 = LNoutweight_conditional_hat2
gen weight1 = aum_assetclass1/aum_tot1
gen weight2 = aum_assetclass2/aum_tot1
gen both = 0
replace both = 1 if weight1 > 0.05 & weight2 > 0.05 & type_new2 != 5
replace both = 1 if weight1 > 0.08 & weight2 > 0.08 & type_new2 == 5

gen _b_LNoutweight_conditional1 = .
gen _b_LNoutweight_conditional2 = .
gen _se_LNoutweight_conditional1 = .
gen _se_LNoutweight_conditional2 = .
gen num_obs = .

eststo clear 

/* Estimation */
forval i=1(1)7{

	capture noisily eststo: ivreghdfe LNweight_assetclass (LNoutweight_conditional1 LNoutweight_conditional2 = LNoutweight_conditional_hat1 LNoutweight_conditional_hat2) [aw=aum_tot1] if type_new2==`i' & both == 1, absorb(fundid) cl(date)
	
	replace _b_LNoutweight_conditional1 = _b[LNoutweight_conditional1] if type_new2==`i'
	replace _b_LNoutweight_conditional2 = _b[LNoutweight_conditional2] if type_new2==`i'
	replace _se_LNoutweight_conditional1 = _se[LNoutweight_conditional1] if type_new2==`i'
	replace _se_LNoutweight_conditional2 = _se[LNoutweight_conditional2] if type_new2==`i'
	replace num_obs = e(N) if type_new2==`i'

}

label var LNoutweight_conditional1 "$\textit{Investment Grade}$"  
label var LNoutweight_conditional2 "$ \textit{High Yield}$" 

/* Reporting */
esttab using "${revisionoutput}/across_results_alt6_pooled_fs_robustness_ins", replace f booktabs ///
b(%9.3f) se(%9.3f) ar2(%9.3f) eqlabels(none) title(xx) ///
mgroups("Life" "P\&C" "Traditional" "Variable Annuity" "ETF" "Others \& Pension" "Foreign", pattern(1 1 1 1 1 1 1) prefix(\multicolumn{@span}{c}{) suffix(}) span erepeat(\cmidrule(lr){@span})) ///
stats(N widstat, fmt(0 %9.2f) ///
labels("Observations" "Kleibergen-Paap F-statistic")) ///
star(* 0.10 ** 0.05 *** 0.01) ///
keep(LNoutweight_conditional1 LNoutweight_conditional2) ///
obslast label ///
addnotes(Standard errors are clustered at the bond level) ///
nonumbers mtitle("I" "II" "III" "IV" "V" "VI" "VII" "VIII" "IX" "X" "XI" "XII") ///
substitute(\_ _)
eststo clear
